#include "homecountwidget.h"
#include "ui_homecountwidget.h"
#include "company.h"

HomeCountWidget::HomeCountWidget(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::HomeCountWidget)
{
    ui->setupUi(this);
}

HomeCountWidget::~HomeCountWidget()
{
    delete ui;
}

void HomeCountWidget::setWidgetType(CountType widgetType){
    this->widgetType = widgetType;
    ui->lineEditCountValue->setText("0");
    if(this->widgetType == CountType::TicketCount){
        //发票盘点
        setWindowTitle("发票盘点");
        ui->labelOldValue->setText("发票总额");
        ui->lineEditOldValue->setText(QString("%1").arg(Company::getTotalTicket()));
        ui->lineEditNewValue->setText(QString("%1").arg(Company::getTotalTicket()));
    }else{
        //资金盘点
        setWindowTitle("资金盘点");
        ui->labelOldValue->setText("资金总额");
        ui->lineEditOldValue->setText(QString("%1").arg(Company::getTotalMoney()));
        ui->lineEditNewValue->setText(QString("%1").arg(Company::getTotalMoney()));
    }
}

void HomeCountWidget::on_pushButtonCancel_clicked()
{
    this->close();
}

void HomeCountWidget::on_lineEditCountValue_editingFinished()
{
    double countValue = ui->lineEditCountValue->text().toDouble();
    ui->lineEditCountValue->setText(QString("%1").arg(countValue));
    double oldValue = ui->lineEditOldValue->text().toDouble();
    double newValue = oldValue+countValue;
    ui->lineEditNewValue->setText(QString("%1").arg(newValue));
}

void HomeCountWidget::on_pushButtonSubmit_clicked()
{
    if(userService.inputOperatePassword(this) == false){
        return;
    }
    companyService.homeCount(
                this,
                ui->pushButtonSubmit,
                widgetType,
                ui->lineEditOldValue->text().toDouble(),
                ui->lineEditCountValue->text().toDouble());
}
